package com.yjhb.android.sbz.dialog

import android.app.Activity
import android.graphics.Color
import android.view.View
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.yjhb.android.sbz.R
import com.yjhb.android.sbz.entity.Comment
import com.yjhb.android.sbz.tool.EventMsg
import com.yjhb.android.sbz.entity.MyTask
import com.yjhb.android.sbz.enums.TaskStatus
import com.yjhb.android.sbz.manager.ResManager
import com.yjhb.android.sbz.mvp.comment.CommentPresenter
import com.yjhb.android.sbz.mvp.comment.CommentView
import com.yjhb.android.sbz.tool.LoadingDialogTool
import com.jojo.android.zxlib.base.BaseDialog
import com.jojo.android.zxlib.tool.ZxTimeTool
import com.jojo.android.zxlib.tool.ZxToast
import com.jojo.android.zxlib.view.flowlayout.FlowLayout
import com.jojo.android.zxlib.view.flowlayout.TagAdapter
import com.jojo.android.zxlib.view.flowlayout.TagView
import kotlinx.android.synthetic.main.dialog_comment.*
import org.greenrobot.eventbus.EventBus
import java.util.*


/**
 *  创建者 zx
 *  创建时间 2021/3/22 11:13
 */
class CommentDialog : BaseDialog, CommentView {
    private var task: MyTask? = null
    private val stars = listOf("审核快", "悬赏主态度好", "任务简单", "要求明确",
            "要求不明确", "超时风险", "审核久", "闪图不好截屏","态度恶劣")
    private val presenter = lazy { CommentPresenter(this) }
    private val loading = LoadingDialogTool.createDialog(activity)


    constructor(fragment: Fragment, task: MyTask) : super(fragment) {
        this.task = task
    }

    constructor(activity: Activity, task: MyTask) : super(activity) {
        this.task = task
    }

    override fun init() {
    }

    /**
     * 将初始化操作放到init代码块，而不是放到init方法中，因为在kotlin中，
     * 构造函数的初始化早于成员变量的初始化。
     * java类成员初始化顺序：静态变量->静态代码块->类成员->类代码块->构造方法
     *
     * kotlin类成员初始化顺序）：
     *      父类主构造器->子类主构造器（如果不存在子类主构造器，次构造器必须委托到父类构造器，因此父类构造器仍会
     *      率先执行，然后才会执行子类的成员变量以及成员代码块，这里在init代码块中执行初始化而不是在init方法中执行初始化
     *      ，就是因为init方法是被父类的构造器所调用，但此时的成员变量还没有完成初始化）
     *      构造函数在成员变量初始化之后执行
     *      init与成员变量初始化按照代码位置顺序执行
     */
    init {
        simpleRatingBar.setOnRatingBarChangeListener { simpleRatingBar, rating, _ ->
            //如果点击到0星的时候设置成1星
            if (rating == 0f) {
                simpleRatingBar?.let {
                    it.rating = 1f
                }
            }
        }
        val dp = ResManager.dp()
        flowLayout?.adapter = object : TagAdapter<String?>(stars) {
            override fun getView(parent: FlowLayout?, position: Int, t: String?): View {
                val textView = TextView(activity)
                textView.text = t
                textView.setTextColor(Color.parseColor("#999999"))
                textView.background = activity.getDrawable(R.drawable.tag_comment)
                textView.textSize = 14f
                textView.setPadding(8 * dp, 3 * dp, 8 * dp, 3 * dp)
                return textView
            }
        }
        flowLayout.setOnTagClickListener { _, _, parent ->
            for (i in 0 until parent.childCount) {
                val tagView: TagView = parent.getChildAt(i) as TagView
                if (tagView.isChecked) {
                    (tagView.tagView as TextView).setTextColor(Color.WHITE)
                } else {
                    (tagView.tagView as TextView).setTextColor(Color.parseColor("#999999"))
                }
            }
            true
        }
        cancel_iv.setOnClickListener {
            dismiss()
        }
        submit_tv.setOnClickListener {
            val set = flowLayout.selectedList
            if (set == null || set.size < 1) {
                ZxToast.showINFOBottomToast("请选择评价哦~")
                return@setOnClickListener
            }
            val comment = Comment()
            comment.createTime = ZxTimeTool.date2String(Date())
            comment.star = simpleRatingBar.rating.toInt()
            task?.run {
                comment.taskId = this.taskId
                comment.orderId = this.id
            }
            if (set.firstOrNull() == null) {
                ZxToast.showINFOBottomToast("请选择评价哦~")
                return@setOnClickListener
            } else {
                comment.content = stars[set.firstOrNull()!!]
            }
            presenter.value.comment(comment)
        }
    }


    override fun getLayoutId() = R.layout.dialog_comment


    override fun onWait() {
        loading.show()
    }

    override fun onSuccess() {
        loading.cancel()
        dismiss()
        ZxToast.showSuccessBottomToast("评价成功！")
        EventBus.getDefault().post(EventMsg(EventMsg.REQUEST_MY_TASK_LIST_UPDATE, TaskStatus.TASK_STATUS_PASS))
    }

    override fun onFailure(code: Int, msg: String?) {
        loading.cancel()
        ZxToast.showSuccessBottomToast(msg)
    }
}